<?php

/**
 * skill actions.
 *
 * @package    jiazhang
 * @subpackage skill
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class skillActions extends sfActions
{

  public function executeEditor(sfWebRequest $request){
     
      
      
  }    
    
  public function executeAdd(sfWebRequest $request){
     
      
      
  }  
    
  public function executeEdit(sfWebRequest $request){
      $skill_id = (int)$request->getParameter('id',0);
      $this->forward404Unless($this->skill = Doctrine::getTable('NewSkill')->findOneById($skill_id));
      $user_id = $this->getUser()->getAttribute('user_id',0);
      $this->forward404Unless( ( $this->skill->getUserId() == $user_id ) );
  }
  
public function executeUpdate(sfWebRequest $request){
      
      if($request->isMethod('POST') && $this->getUser()->isAuthenticated()){
          $skill_id = (int)$request->getParameter('skill_id',0);
          $user_id = $this->getUser()->getAttribute('user_id',0);
          $title = strip_tags($request->getParameter('title',''));
          $blocks = $request->getParameter('smallcont',array());
          $cover = $request->getParameter('homeimg','');
          $description = $request->getParameter('docsmallt','');
          $category = $request->getParameter('gotta',array());
          $age = $request->getParameter('age',array());
          $tags = $request->getParameter('ability',array());
          $status = $request->getParameter('status',0); //判断是否为草稿
          $content = $request->getParameter('content','');
          rsort($category);
          $category = (int)$category[0];
          rsort($age);
          $age_range = NewSkill::$age_range[(int)$age[0]];
          
          $skill = Doctrine::getTable('NewSkill')->findOneById($skill_id);
          
          if(mb_strlen($title) > 100 ){
              $title = mb_substr($title,0,100);
          }
          
          if(mb_strlen($description) > 75){
              $description = mb_substr($description,0,75);
          }
          
          $skill->setTitle($title)
                ->setArea('')
                ->setCover($cover)
                ->setEditContent($content)
                ->setCategory($category)
                ->setMinAge($age_range['min'])
                ->setMaxAge($age_range['max'])
//                ->setStatus($status)
                ->setDescription($description)
                ->setUserId($user_id)
                ->save();
//         $skill->createStatistical(); 
          
         $skill_id = $skill->getId();
         Doctrine::getTable('NewSkillTag')->createQuery()->where('skill_id = ?',$skill_id)->delete()->execute();
         
         foreach($tags as $tag){
             if(strlen(trim($tag)) == 0) continue;
             
             $skill_tag = new NewSkillTag();
             $skill_tag->setTagName($tag)->setSkillId($skill_id)->save();
         }
      
      Doctrine::getTable('Block')->createQuery()->where('skill_id = ?',$skill_id)->delete()->execute();
      $i = 1;
      foreach($blocks as $block){
          
          $block = explode('+===+',$block);
          $type = $block[0];
          $content = $block[1];
          $block = new Block();
          $block->setBType(Block::$b_type_num[$type])->setContent($content)->setSkillId($skill_id)->setSortNum($i)->save();
          $i++;
          
      }
      
      $this->redirect('skill/personal?user_id='.$user_id);
      
      }
      
      $this->redirect('skill/index');
      
  }
  
  public function executeCreate(sfWebRequest $request){
      
      if($request->isMethod('POST') && $this->getUser()->isAuthenticated()){
          
          $user_id = $this->getUser()->getAttribute('user_id',0);
          $title = strip_tags($request->getParameter('title',''));
          $blocks = $request->getParameter('smallcont',array());
          $cover = $request->getParameter('homeimg','');
          $description = $request->getParameter('docsmallt','');
          $category = $request->getParameter('gotta',array());
          $age = $request->getParameter('age',array());
          $tags = $request->getParameter('ability',array());
          $status = $request->getParameter('status',0); //判断是否为草稿
          $content = $request->getParameter('content','');
          rsort($category);
          $category = $category[0];
          rsort($age);
          $age_range = NewSkill::$age_range[$age[0]];
          
          if(strlen($title) > 100 ){
              $title = substr($title,0,100);
          }
          
          if(strlen($description) > 75){
              $description = substr($description,0,75);
          }
          
          $skill = new NewSkill();
          $skill->setTitle($title)
                ->setArea('')
                ->setCover($cover)
                ->setEditContent($content)
                ->setCategory($category)
                ->setMinAge($age_range['min'])
                ->setMaxAge($age_range['max'])
                ->setStatus($status)
                ->setDescription($description)
                ->setUserId($user_id)
                ->save();
         $skill->createStatistical(); 
         $skill_id = $skill->getId();
         
         foreach($tags as $tag){
             if(strlen(trim($tag)) == 0) continue;
             
             $skill_tag = new NewSkillTag();
             $skill_tag->setTagName($tag)->setSkillId($skill_id)->save();
         }
         
        $i = 1;
        foreach($blocks as $block){

            $block = explode('+===+',$block);
            $type = $block[0];
            $content = $block[1];
            $block = new Block();
            $block->setBType(Block::$b_type_num[$type])->setContent($content)->setSkillId($skill_id)->setSortNum($i)->save();
            $i++;
        }

        $this->redirect('skill/personal?user_id='.$user_id);
      
      }
      
      $this->redirect('skill/index');
      
  }
  
  public function executeUpload_img(sfWebRequest $request){
        sfConfig::set('sf_web_debug',false);
      if($request->isMethod('POST')){
//          $is_cover = (int)$request->getParameter('is_cover',0);
          $file = $request->getFiles('imgFile');
          $file_name = $file['name'];
          $file_ext_tmp = explode('.', $file_name);
          $file_ext = $file_ext_tmp[count($file_ext_tmp)-1];
          $allow_file_type = array('jpg','bmp','png','gif','jpeg');
          
          if(in_array($file_ext, $allow_file_type)){
             $year = date('Y',time());
             $month = date('m',time());
             $day = date('d',time());
             $save_file_name = $year.'_'.$month.'_'.$day.'_'.substr(md5(time()+rand(0,999)),25).'.'.$file_ext;
             $upyun_save_path = '/skill/'.$year.'/'.$month.'/'.$day.'/'.$save_file_name;
             $upyun = new UpYun(sfConfig::get('app_upyun_space'),sfConfig::get('app_upyun_user') , sfConfig::get('app_upyun_pwd'));
             
            try {
                $fh = fopen($file['tmp_name'], 'rb');
                $rsp = $upyun->writeFile($upyun_save_path, $fh, True);   // 上传图片，自动创建目录
                fclose($fh);
            }
            catch(Exception $e) {
                echo $e->getCode();
                echo $e->getMessage();
            }
            
            $image_url=  sfConfig::get('app_img_url').$upyun_save_path;
            $json = new Services_JSON();
            $return_data = array('error' => 0, 'url' => $image_url);
            return $this->renderText($json->encode($return_data));
            exit();
          }else{
              
            if($is_cover){
                $return_data = array('errorCode' => 404,'errorMsg'=> '文件类型错误！');
            }else{
                $return_data = array('error' => 1, 'url' => '文件类型错误！');
            }
              //error 文件类型错误
              $json = new Services_JSON();
              return $this->renderText($json->encode($return_data));
              exit();
          }
          
      }
  }  
  
  
  public function executeIndex(sfWebRequest $request)
  {

  }
  
  public function executeIndex_page(sfWebRequest $request){

        
        $page = (int)$request->getParameter('page',1);
        $type = (int)$request->getParameter('type',20);
        
        if($type > 10 && $type != 99){
            $dql = Doctrine::getTable('NewSkill')->createQuery()->where('status = 2')->orderBy('id desc');
        }else{
            $dql = Doctrine::getTable('NewSkill')->createQuery()->where('category = ?',$type)->andWhere('status = 1 or status = 2')->orderBy('id desc');
        }
        
        if($this->getUser()->isAuthenticated() && $type == 99){
            $user_id = $this->getUser()->getAttribute('user_id',0);
            
            $user_ids = Doctrine::getTable('UserAttention')->createQuery()->where('action_user_id = ?',$user_id)->execute()->toKeyValueArray('attention_user_id', 'attention_user_id');
            $dql = Doctrine::getTable('NewSkill')->createQuery()->whereIn('user_id',$user_ids)->andWhere('status = 1 or status = 2')->orderBy('id desc');
            
        }
        
        $this->pager = new sfDoctrinePager('NewSkill',15);
        $this->pager->setQuery($dql);
        $this->pager->setPage($page);
        $this->pager->init();
        
        if( count($this->pager->getResults()) == 0){
            	$arr = array(
		"success"=>true,
		"message"=>'No more pictures', //当没有值时回调：No more pictures
		"data"=>array(),
                );
        }else{
            	$arr = array(
                    "success"=>true,
                    "message"=>'Yes Img', //当没有值时回调：No more pictures
                    "data"=>array()
                 );
                
                $i=0;
            foreach($this->pager->getResults() as $skill){
                $arr['data'][$i]['urlink'] = $this->generateUrl('', array('module'=>'skill','action'=>'show','id'=>$skill->getId()));
                $arr['data'][$i]['title'] = $skill->getTitle();
                $arr['data'][$i]['preview'] = $skill->getCover();
                $arr['data'][$i]['userpto'] = $skill->getUser()->getCover();
                $arr['data'][$i]['userna'] = $skill->getUser()->getNickname();
                $arr['data'][$i]['userlink'] =  $this->generateUrl('', array('module'=>'skill','action'=>'personal','user_id'=>$skill->getUserId()));
                $arr['data'][$i]['collectnum'] = $skill->getStatistical()->getCommentNum();
                $arr['data'][$i]['reviewnum'] = $skill->getStatistical()->getLikeNum();
                $i++;
            }
            
        }
        
        return $this->renderText(json_encode($arr));
  }
  
  
  public function executeShow(sfWebRequest $request){
      $this->forward404Unless($this->skill = Doctrine::getTable('NewSkill')->findOneById($id = $request->getParameter('id',0)));
      $look_num = $this->skill->getStatistical()->getLookNum()+1;
      $this->skill->getStatistical()->setLookNum($look_num)->save();
  }
  
  public function executeLike(sfWebRequest $request){
      if($request->isMethod('POST')){
          
          $skill_id = $request->getParameter('skill_id',0);
          $user_id = $this->getUser()->getAttribute('user_id',0);
          
          $msg = array('error'=>0,'msg'=>'点赞成功！');
          
          $has_fav = Doctrine::getTable('SkillLikeRecord')->createQuery()->where('user_id = ?',$user_id)->andWhere('skill_id = ?',$skill_id)->count();
          
          if($this->getUser()->isAuthenticated()){
              
            if($has_fav > 0){
                $msg['error'] = 1;
                $msg['msg'] = '您已经点过赞了！';
            }else{
                $new_skill_statistical = Doctrine::getTable('NewSkillStatistical')->findOneBySkillId($skill_id);
                $like_num = $new_skill_statistical->getLikeNum()+1;
                $new_skill_statistical->setLikeNum($like_num)->save();
                $like_record = new SkillLikeRecord();
                $like_record->setUserId($user_id)->setSkillId($skill_id)->save();
            }
          }else{
              $msg = array('error'=>1,'msg'=>'请先登入！');
          }
          
          return $this->renderText(json_encode($msg));
      }
      
  }
  
  
  public function executeFav(sfWebRequest $request){
      if($request->isMethod('POST')){
          
          $skill_id = $request->getParameter('skill_id',0);
          $user_id = $this->getUser()->getAttribute('user_id',0);
          $msg = array('error'=>0,'msg'=>'收藏成功！');
          
          if(!$this->getUser()->isAuthenticated()){
              $msg['error'] = 1;
              $msg['msg'] = '请先登入！';
              return $this->renderText(json_encode($msg));
          }
          
          $has_fav = Doctrine::getTable('SkillFavoriteRecord')->createQuery()->where('user_id = ?',$user_id)->andWhere('skill_id = ?',$skill_id)->count();
          
          if($has_fav > 0){
              $msg['error'] = 1;
              $msg['msg'] = '您已经收藏了此技能';
          }else{
              $fav = new SkillFavoriteRecord();
              $fav->setUserId($user_id)->setSkillId($skill_id)->save();
          }
          
          return $this->renderText(json_encode($msg));
      }
      
  }
  
    public function executeManage_skill(sfWebRequest $request){
      $user_id = (int)$request->getParameter('user_id',0);
      $this->forward404Unless($this->user = Doctrine::getTable('User')->findOneById($user_id));
      
      if($user_id != $this->getUser()->getAttribute('user_id')){
          $this->redirect('skill/index');
      }
      
  }
  
  public function executeManage_skill_data(sfWebRequest $request){
      
//      if($request->isMethod('POST')){
          
          $arr = array();
          $user_id = (int)$request->getParameter('user_id',0);
          $this->forward404Unless($user = Doctrine::getTable('User')->findOneById($user_id));
          
          if($user_id == 0){
            $arr = array(
		"success"=>true,
		"message"=>'No more pictures', //当没有值时回调：No more pictures
		"data"=>array(),
                );
          }
          
          
        $page = (int)$request->getParameter('page',1);
        $dql = Doctrine::getTable('NewSkill')->createQuery()->where('user_id = ?',$user_id)->orderBy('id desc');
        
        $this->pager = new sfDoctrinePager('NewSkill',15);
        $this->pager->setQuery($dql);
        $this->pager->setPage($page);
        $this->pager->init();
        
       
        
        if( count($this->pager->getResults()) == 0){
            	$arr = array(
		"success"=>true,
		"message"=>'No more pictures', //当没有值时回调：No more pictures
		"data"=>array(),
                );
        }else{
            	$arr = array(
                    "success"=>true,
                    "message"=>'Yes Img', //当没有值时回调：No more pictures
                    "data"=>array()
                 );
                
                $i=0;
            foreach($this->pager->getResults() as $skill){
                $arr['data'][$i]['urlink'] = $this->generateUrl('', array('module'=>'skill','action'=>'show','id'=>$skill->getId()));
                $arr['data'][$i]['title'] = $skill->getTitle();
                $arr['data'][$i]['preview'] = $skill->getCover();
                $arr['data'][$i]['editurl'] = $this->generateUrl('',array('module'=>'skill','action'=>'edit','id'=>$skill->getId()));
                $arr['data'][$i]['delurl'] = $this->generateUrl('',array('module'=>'skill','action'=>'delete','id'=>$skill->getId()));;
                $arr['data'][$i]['collectnum'] = $skill->getStatistical()->getLikeNum();
                $arr['data'][$i]['reviewnum'] = $skill->getStatistical()->getCommentNum();
                $i++;
            }
            
        }
          
      return $this->renderText(json_encode($arr));
  }
  
  public function executeDelete(sfWebRequest $request){
      $this->forward404Unless($skill = Doctrine::getTable('NewSkill')->findOneById($id = $request->getParameter('id',0)));
      $skill->delete();
      $this->redirect('skill/manage_skill?user_id='.$this->getUser()->getAttribute('user_id'));
  }
  
  public function executeMy_comment(sfWebRequest $request){
      $user_id = (int)$request->getParameter('user_id',0);
      $this->forward404Unless($this->user = Doctrine::getTable('User')->findOneById($user_id));
  }
  
  public function executeMy_comment_data(sfWebRequest $request){
      
//      if($request->isMethod('POST')){
          
          $arr = array();
          $user_id = (int)$request->getParameter('user_id',0);
          $this->forward404Unless($user = Doctrine::getTable('User')->findOneById($user_id));
          
          if($user_id == 0){
            $arr = array(
		"success"=>true,
		"message"=>'No more pictures', //当没有值时回调：No more pictures
		"data"=>array(),
                );
          }
          
          
        $page = (int)$request->getParameter('page',1);
        
        $skill_ids = Doctrine::getTable('SkillComment')->createQuery()->where('user_id = ?',$user_id)->execute()->toKeyValueArray('skill_id', 'skill_id');
        array_unique($skill_ids);

        $dql = Doctrine::getTable('NewSkill')->createQuery()->whereIn('id',$skill_ids)->andWhere('status = 1 or status = 2')->orderBy('id desc');
        $this->pager = new sfDoctrinePager('NewSkill',15);
        $this->pager->setQuery($dql);
        $this->pager->setPage($page);
        $this->pager->init();
        
       
        
        if( count($this->pager->getResults()) == 0){
            	$arr = array(
		"success"=>true,
		"message"=>'No more pictures', //当没有值时回调：No more pictures
		"data"=>array(),
                );
        }else{
            	$arr = array(
                    "success"=>true,
                    "message"=>'Yes Img', //当没有值时回调：No more pictures
                    "data"=>array()
                 );
                
            $i=0;
            foreach($this->pager->getResults() as $skill){
                $arr['data'][$i]['urlink'] = $this->generateUrl('', array('module'=>'skill','action'=>'show','id'=>$skill->getId()));
                $arr['data'][$i]['title'] = $skill->getTitle();
                $arr['data'][$i]['preview'] = $skill->getCover();
                $arr['data'][$i]['looknum'] = $skill->getStatistical()->getLookNum();
                $arr['data'][$i]['collectnum'] = $skill->getStatistical()->getLikeNum();
                $arr['data'][$i]['reviewnum'] = $skill->getStatistical()->getCommentNum();
                $i++;
            }
            
        }
          
      return $this->renderText(json_encode($arr));
  }
  
    public function executeMy_like(sfWebRequest $request){
      $user_id = (int)$request->getParameter('user_id',0);
      $this->forward404Unless($this->user = Doctrine::getTable('User')->findOneById($user_id));
  }
  
  public function executeMy_like_data(sfWebRequest $request){
      
//      if($request->isMethod('POST')){
          
          $arr = array();
          $user_id = (int)$request->getParameter('user_id',0);
          $this->forward404Unless($user = Doctrine::getTable('User')->findOneById($user_id));
          
          if($user_id == 0){
            $arr = array(
		"success"=>true,
		"message"=>'No more pictures', //当没有值时回调：No more pictures
		"data"=>array(),
                );
          }
          
        $page = (int)$request->getParameter('page',1);
        $skill_ids = Doctrine::getTable('SkillLikeRecord')->createQuery()->where('user_id = ?',$user_id)->execute()->toKeyValueArray('skill_id', 'skill_id');
        array_unique($skill_ids);
        
        if(count($skill_ids) == 0){
            $skill_ids = 0;
        }
        
        $dql = Doctrine::getTable('NewSkill')->createQuery()->whereIn('id',$skill_ids)->andWhere('status = 1 or status = 2')->orderBy('id desc');
        $this->pager = new sfDoctrinePager('NewSkill',15);
        $this->pager->setQuery($dql);
        $this->pager->setPage($page);
        $this->pager->init();
        
       
        
        if( count($this->pager->getResults()) == 0){
            	$arr = array(
		"success"=>true,
		"message"=>'No more pictures', //当没有值时回调：No more pictures
		"data"=>array(),
                );
        }else{
            	$arr = array(
                    "success"=>true,
                    "message"=>'Yes Img', //当没有值时回调：No more pictures
                    "data"=>array()
                 );
                
            $i=0;
            foreach($this->pager->getResults() as $skill){
                $arr['data'][$i]['urlink'] = $this->generateUrl('', array('module'=>'skill','action'=>'show','id'=>$skill->getId()));
                $arr['data'][$i]['title'] = $skill->getTitle();
                $arr['data'][$i]['preview'] = $skill->getCover();
                $arr['data'][$i]['looknum'] = $skill->getStatistical()->getLookNum();
                $arr['data'][$i]['collectnum'] = $skill->getStatistical()->getLikeNum();
                $arr['data'][$i]['reviewnum'] = $skill->getStatistical()->getCommentNum();
                $i++;
            }
            
        }
          
      return $this->renderText(json_encode($arr));
  }
  
  
  
 public function executeMy_fav(sfWebRequest $request){
      $user_id = (int)$request->getParameter('user_id',0);
      $this->forward404Unless($this->user = Doctrine::getTable('User')->findOneById($user_id));
  }
  
  public function executeMy_fav_data(sfWebRequest $request){
      
//      if($request->isMethod('POST')){
          
          $arr = array();
          $user_id = (int)$request->getParameter('user_id',0);
          $this->forward404Unless($user = Doctrine::getTable('User')->findOneById($user_id));
          
          if($user_id == 0){
            $arr = array(
		"success"=>true,
		"message"=>'No more pictures', //当没有值时回调：No more pictures
		"data"=>array(),
                );
          }
          
          
        $page = (int)$request->getParameter('page',1);
        $skill_id = Doctrine::getTable('SkillFavoriteRecord')->createQuery()->where('user_id = ?',$user_id)->execute()->toKeyValueArray('skill_id', 'skill_id');
        
        if(count($skill_id) == 0 ){
            $skill_id = 0;
        }
        
        $dql = Doctrine::getTable('NewSkill')->createQuery()->whereIn('id',$skill_id)->andWhere('status = 1 or status = 2')->orderBy('id desc');
        
        $this->pager = new sfDoctrinePager('NewSkill',15);
        $this->pager->setQuery($dql);
        $this->pager->setPage($page);
        $this->pager->init();
        
       
        
        if( count($this->pager->getResults()) == 0){
            	$arr = array(
		"success"=>true,
		"message"=>'No more pictures', //当没有值时回调：No more pictures
		"data"=>array(),
                );
        }else{
            	$arr = array(
                    "success"=>true,
                    "message"=>'Yes Img', //当没有值时回调：No more pictures
                    "data"=>array()
                 );
                
            $i=0;
            foreach($this->pager->getResults() as $skill){
                $arr['data'][$i]['urlink'] = $this->generateUrl('', array('module'=>'skill','action'=>'show','id'=>$skill->getId()));
                $arr['data'][$i]['title'] = $skill->getTitle();
                $arr['data'][$i]['preview'] = $skill->getCover();
                $arr['data'][$i]['looknum'] = $skill->getStatistical()->getLookNum();
                $arr['data'][$i]['collectnum'] = $skill->getStatistical()->getLikeNum();
                $arr['data'][$i]['reviewnum'] = $skill->getStatistical()->getCommentNum();
                $i++;
            }
            
        }
          
      return $this->renderText(json_encode($arr));
  }
  
  
public function executeAttention_me(sfWebRequest $request){
      $user_id = (int)$request->getParameter('user_id',0);
      $this->forward404Unless($this->user = Doctrine::getTable('User')->findOneById($user_id));
  }
  
  public function executeAttention_me_data(sfWebRequest $request){
      
//      if($request->isMethod('POST')){
          
          $arr = array();
          $user_id = (int)$request->getParameter('user_id',0);
          $this->forward404Unless($user = Doctrine::getTable('User')->findOneById($user_id));
          
          if($user_id == 0){
            $arr = array(
		"success"=>true,
		"message"=>'No more pictures', //当没有值时回调：No more pictures
		"data"=>array(),
                );
          }
          
          
        $page = (int)$request->getParameter('page',1);
        
        $user_ids = Doctrine::getTable('UserAttention')->createQuery()->where('attention_user_id = ?',$user_id)->execute()->toKeyValueArray('action_user_id', 'action_user_id');
        if(count($user_ids) == 0){
            $user_ids = 0;
        }
        
        $dql = Doctrine::getTable('NewSkill')->createQuery()->whereIn('user_id',$user_ids)->andWhere('status = 1 or status = 2')->orderBy('id desc');
        
        $this->pager = new sfDoctrinePager('NewSkill',15);
        $this->pager->setQuery($dql);
        $this->pager->setPage($page);
        $this->pager->init();
        
       
        
        if( count($this->pager->getResults()) == 0){
            	$arr = array(
		"success"=>true,
		"message"=>'No more pictures', //当没有值时回调：No more pictures
		"data"=>array(),
                );
        }else{
            	$arr = array(
                    "success"=>true,
                    "message"=>'Yes Img', //当没有值时回调：No more pictures
                    "data"=>array()
                 );
                $i=0;
            foreach($this->pager->getResults() as $skill){
                $arr['data'][$i]['urlink'] = $this->generateUrl('', array('module'=>'skill','action'=>'show','id'=>$skill->getId()));
                $arr['data'][$i]['title'] = $skill->getTitle();
                $arr['data'][$i]['preview'] = $skill->getCover();
                $arr['data'][$i]['looknum'] = $skill->getStatistical()->getLooknNum();
                $arr['data'][$i]['collectnum'] = $skill->getStatistical()->getCommentNum();
                $arr['data'][$i]['reviewnum'] = $skill->getStatistical()->getLikeNum();
                $i++;
            }
            
        }
          
      return $this->renderText(json_encode($arr));
  }
  
  
public function executeMy_attention(sfWebRequest $request){
      $user_id = (int)$request->getParameter('user_id',0);
      $this->forward404Unless($this->user = Doctrine::getTable('User')->findOneById($user_id));
  }
  
  public function executeMy_attention_data(sfWebRequest $request){
      
//      if($request->isMethod('POST')){
          
          $arr = array();
          $user_id = (int)$request->getParameter('user_id',0);
          $this->forward404Unless($user = Doctrine::getTable('User')->findOneById($user_id));
          
          if($user_id == 0){
            $arr = array(
		"success"=>true,
		"message"=>'No more pictures', //当没有值时回调：No more pictures
		"data"=>array(),
                );
          }
          
          
        $page = (int)$request->getParameter('page',1);
        $user_ids = Doctrine::getTable('UserAttention')->createQuery()->where('action_user_id = ?',$user_id)->execute()->toKeyValueArray('attention_user_id', 'attention_user_id'); 
        if(count($user_ids) == 0){
            $user_ids = 0;
        }
        $dql = Doctrine::getTable('NewSkill')->createQuery()->whereIn('user_id',$user_ids)->andWhere('status = 1 or status = 2')->orderBy('id desc');
        
        $this->pager = new sfDoctrinePager('NewSkill',15);
        $this->pager->setQuery($dql);
        $this->pager->setPage($page);
        $this->pager->init();
        
        if( count($this->pager->getResults()) == 0){
            	$arr = array(
		"success"=>true,
		"message"=>'No more pictures', //当没有值时回调：No more pictures
		"data"=>array(),
                );
        }else{
            	$arr = array(
                    "success"=>true,
                    "message"=>'Yes Img', //当没有值时回调：No more pictures
                    "data"=>array()
                 );
                $i=0;
            foreach($this->pager->getResults() as $skill){
                $arr['data'][$i]['urlink'] = $this->generateUrl('', array('module'=>'skill','action'=>'show','id'=>$skill->getId()));
                $arr['data'][$i]['title'] = $skill->getTitle();
                $arr['data'][$i]['preview'] = $skill->getCover();
                $arr['data'][$i]['looknum'] = $skill->getStatistical()->getLookNum();
                $arr['data'][$i]['collectnum'] = $skill->getStatistical()->getLikeNum();
                $arr['data'][$i]['reviewnum'] = $skill->getStatistical()->getCommentNum();
                $i++;
            }
            
        }
          
      return $this->renderText(json_encode($arr));
  }
  
  public function executePersonal(sfWebRequest $request){
      $user_id = (int)$request->getParameter('user_id',0);
      $this->forward404Unless($this->user = Doctrine::getTable('User')->findOneById($user_id));
  }
  
  public function executePersonal_default_data(sfWebRequest $request){
      
//      if($request->isMethod('POST')){
          
          $arr = array();
          $user_id = (int)$request->getParameter('user_id',0);
          $this->forward404Unless($user = Doctrine::getTable('User')->findOneById($user_id));
          
          if($user_id == 0){
            $arr = array(
		"success"=>true,
		"message"=>'No more pictures', //当没有值时回调：No more pictures
		"data"=>array(),
                );
          }
          
          
        $page = (int)$request->getParameter('page',1);
        $dql = Doctrine::getTable('NewSkill')->createQuery()->where('user_id = ?',$user_id)->andWhere('status <> 3 or status <> 5')->orderBy('id desc');
        
        $this->pager = new sfDoctrinePager('NewSkill',15);
        $this->pager->setQuery($dql);
        $this->pager->setPage($page);
        $this->pager->init();
        
       
        
        if( count($this->pager->getResults()) == 0){
            	$arr = array(
		"success"=>true,
		"message"=>'No more pictures', //当没有值时回调：No more pictures
		"data"=>array(),
                );
        }else{
            	$arr = array(
                    "success"=>true,
                    "message"=>'Yes Img', //当没有值时回调：No more pictures
                    "data"=>array()
                 );
                
                $i=0;
            foreach($this->pager->getResults() as $skill){
                $arr['data'][$i]['urlink'] = $this->generateUrl('', array('module'=>'skill','action'=>'show','id'=>$skill->getId()));
                $arr['data'][$i]['title'] = $skill->getTitle();
                $arr['data'][$i]['preview'] = $skill->getCover();
                $arr['data'][$i]['looknum'] = $skill->getStatistical()->getLookNum();
                $arr['data'][$i]['collectnum'] = $skill->getStatistical()->getLikeNum();
                $arr['data'][$i]['reviewnum'] = $skill->getStatistical()->getCommentNum();
                $i++;
            }
            
        }
          
      return $this->renderText(json_encode($arr));
  }
  
  public function executeUser_attention(sfWebRequest $request){
        if($request->isMethod('POST')){
           
            $action_user_id = $this->getUser()->getAttribute('user_id',0);
            $attention_user_id = (int)$request->getParameter('user_id',0);
            

            
           if($this->getUser()->isAuthenticated()){
            
            if($action_user_id == $attention_user_id){
                $msg['error'] = 1;
                $msg['msg'] = '您不能关注自己！';
                return $this->renderText(json_encode($msg));
            }
               
            $has_user = Doctrine::getTable('UserAttention')
                    ->createQuery()
                    ->where('action_user_id = ?',$action_user_id)
                    ->andWhere('attention_user_id = ?',$attention_user_id)
                    ->execute();
            
            if(count($has_user) > 0){
                
                $has_user->delete();
                $msg['error'] = 1;
                $msg['msg'] = '您已经取消关注了！';
                $me_stat = UserStatisticalTable::getInstance()->getStatistical($action_user_id);
                $atten_user = UserStatisticalTable::getInstance()->getStatistical($attention_user_id);
                $floow_num = $me_stat->getFloowMe()-1; //我关注的+1
                $fans_num = $atten_user->getFansNum()-1;//对方粉丝+1
                $me_stat->setFloowMe( ($floow_num < 0 ? 0 : $floow_num ) )->save();
                $atten_user->setFansNum( ($fans_num < 0 ? 0 : $fans_num ) )->save();
                $msg['fans'] = $fans_num;
                $msg['attention_people'] = $floow_num;
                return $this->renderText(json_encode($msg));
                
            }else{
                
                $attention = new UserAttention();
                $attention->setActionUserId($action_user_id)->setAttentionUserId($attention_user_id)->save();
                $msg['error'] = 0;
                $msg['msg'] = '您已经关注成功了！';
                $me_stat = UserStatisticalTable::getInstance()->getStatistical($action_user_id);
                $atten_user = UserStatisticalTable::getInstance()->getStatistical($attention_user_id);
                $floow_num = $me_stat->getFloowMe()+1; //我关注的+1
                $fans_num = $atten_user->getFansNum()+1;//对方粉丝+1
                $me_stat->setFloowMe($floow_num)->save();
                $atten_user->setFansNum($fans_num)->save();
                $msg['fans'] = $fans_num;
                $msg['attention_people'] = $floow_num;
                return $this->renderText(json_encode($msg));
                
            }
           }else{
               $msg['error'] = 1;
                $msg['msg'] = '请先登入！';
                return $this->renderText(json_encode($msg));
           }
       }
       
       return $this->renderText(json_encode($msg));
  }
  
  public function executeAdd_comment(sfWebRequest $request){
      if($request->isMethod('POST')){
          $skill_id = $request->getParameter('skill_id',0);
          $content = trim($request->getParameter('content',0));
          
          $skill = Doctrine::getTable('NewSkill')->findOneById($skill_id);
          
          if($skill){

              $block_comment = new SkillComment();
              $user_id = $this->getUser()->getAttribute('user_id',0);
              $block_comment->setSkillId($skill_id)->setDisplay(1)->setContent($content)->setUserId($user_id)->save();
              $this->getUser()->setFlash('success', '评论发布成功！');
              $comment_num = $skill->getStatistical()->getCommentNum()+1;
              $skill->getStatistical()->setCommentNum($comment_num)->save();
          }
          
          $this->redirect('skill/show?id='.$skill_id);
          
      }
  }
  
  public function executeMabe_like_data(sfWebRequest $request){
      
      $page = (int)$request->getParameter('page',0);
      $user_id = $this->getUser()->getAttribute('user_id',0);
      $attention_users = Doctrine::getTable('UserAttention')->createQuery()->where('action_user_id = ?',$user_id)->execute()->toKeyValueArray('attention_user_id', 'attention_user_id');
      $attention_users[] = $user_id;
      $user_sort_ids = Doctrine::getTable('UserStatistical')->createQuery()->whereNotIn('user_id',$attention_users)->orderBy('fans_num desc')->execute()->toKeyValueArray('user_id', 'user_id');
      $dql = Doctrine::getTable('User')->createQuery()->whereIn('id',$user_sort_ids)->andWhere('status = 1');
      
      $this->pager = new sfDoctrinePager('User',5);
      $this->pager->setQuery($dql);
      $this->pager->setPage($page);
      $this->pager->init();
            
  }
  
  
}
